﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using DataBase;
using DBService;
using YArchitech.Revit.MEP.Opening.Common;
using Transfer.CommonUnit;

namespace YArchitech.Revit.MEP.Opening
{
	public class DbWallToBeOpened : DbOpeningBase, IDbObject
	{
		public DbWallToBeOpened(int projectId, XYZ basePt, int wallId, int pipeId, SectionParameter sp, BoxParameter bp, CutType cType, HostType hType, string level)
		{
			this.mGuid = Guid.NewGuid();
			this.mProjectId = projectId;
			this.mHostId = wallId;
			this.mPt = basePt;
			this.mParameter = sp;
			base.BoxParameter = bp;
			base.CutType = cType;
			base.HostType = hType;
			base.Level = level;
			base.Date = DateTime.Now;
			base.IsCurrentProject = true;
			this.mPipeId = pipeId;
		}

		public DbWallToBeOpened(Autodesk.Revit.DB.Document doc, int projectId, XYZ basePt, int wallId, int pipeId, SectionParameter sp, BoxParameter bp, CutType cType, HostType hType, string level)
		{
			this.mGuid = Guid.NewGuid();
			this.mProjectId = projectId;
			this.mHostId = wallId;
			this.mPt = basePt;
			this.mParameter = sp;
			base.BoxParameter = bp;
			base.CutType = cType;
			base.HostType = hType;
			base.Level = level;
			base.Date = DateTime.Now;
			base.IsCurrentProject = true;
			this.mPipeId = pipeId;
			try
			{
				Wall wall = doc.GetElement(new ElementId(wallId)) as Wall;
				this.dWallWidth = wall.WallType.Width;
				this.vtWallOrient = wall.Orientation;
				base.DrivePipeTypeName = "无";
				base.AxisX = "";
				base.AxisY = "";
				base.MEPFileName = "";
				base.PipeStartPoint = XYZ.Zero;
				base.PipeEndPoint = XYZ.Zero;
				base.OpeningID = ElementId.InvalidElementId;
			}
			catch
			{
			}
		}

		public override void WirteToXML(string fileName)
		{
			XmlUtils.AddWallOpening(fileName, this);
		}

		public static List<DbWallToBeOpened> ReadFromXML(string fileName)
		{
			return XmlUtils.ReadWallOpening(fileName);
		}

		public static DbWallToBeOpened ReadFromXML(Guid id, string fileName)
		{
			return XmlUtils.ReadWallOpening(id, fileName);
		}

		public override void UpdateToXML(string fileName)
		{
			XmlUtils.UpdateWallOpeningToXML(fileName, this);
		}

		public static List<DbWallToBeOpened> GetByProject(DbProject project)
		{
			List<DbWallToBeOpened> result;
			try
			{
				if (project.Database.Connection.State == ConnectionState.Closed)
				{
					project.Database.Connection.Open();
				}
				List<DbWallToBeOpened> list = new List<DbWallToBeOpened>();
				string sql = string.Format("SELECT ProjectId,HostId,PtX,PtY,PtZ,Parameter1,Parameter2,SectionType,Guid,Status,BoxParameter1,BoxParameter2,BoxParameter3,BoxParameter4,Level,CutType,HostType,BoxSectionType,Remark,PipeId FROM tbSynergyOpiening WHERE ProjectId={0} and (HostType=0 or HostType=2)", project.Id);
				DataTable dataTable = project.Database.ExecQuery(sql);
				if (project.Database.Connection.State == ConnectionState.Open)
				{
					project.Database.Connection.Close();
				}
				if (dataTable == null)
				{
					result = null;
				}
				else
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						int projectId = Convert.ToInt32(dataTable.Rows[i]["ProjectId"]);
						Guid mGuid = Guid.Parse(dataTable.Rows[i]["Guid"].ToString());
						int wallId = Convert.ToInt32(dataTable.Rows[i]["HostId"]);
						int pipeId = Convert.ToInt32(dataTable.Rows[i]["PipeId"]);
						OpeningStatus status = (OpeningStatus)dataTable.Rows[i]["Status"];
						XYZ basePt = new XYZ(Convert.ToDouble(dataTable.Rows[i]["PtX"]), Convert.ToDouble(dataTable.Rows[i]["PtY"]), Convert.ToDouble(dataTable.Rows[i]["PtZ"]));
						DBService.SectionType sectionType = (DBService.SectionType)Convert.ToInt32(dataTable.Rows[i]["SectionType"]);
						SectionParameter sp = null;
						if (sectionType == DBService.SectionType.Arc)
						{
							sp = new SectionParameter(Convert.ToDouble(dataTable.Rows[i]["Parameter1"]));
						}
						else if (sectionType == DBService.SectionType.Rect)
						{
							double param = Convert.ToDouble(dataTable.Rows[i]["Parameter1"]);
							double param2 = Convert.ToDouble(dataTable.Rows[i]["Parameter2"]);
							sp = new SectionParameter(param, param2);
						}
						DBService.SectionType sectionType2 = (DBService.SectionType)Convert.ToInt32(dataTable.Rows[i]["BoxSectionType"]);
						BoxParameter bp = null;
						if (sectionType2 == DBService.SectionType.Arc)
						{
							bp = new BoxParameter(Convert.ToDouble(dataTable.Rows[i]["BoxParameter1"]));
						}
						else if (sectionType2 == DBService.SectionType.Rect)
						{
							double param3 = Convert.ToDouble(dataTable.Rows[i]["BoxParameter1"]);
							double param4 = Convert.ToDouble(dataTable.Rows[i]["BoxParameter1"]);
							double param5 = Convert.ToDouble(dataTable.Rows[i]["BoxParameter3"]);
							double param6 = Convert.ToDouble(dataTable.Rows[i]["BoxParameter4"]);
							bp = new BoxParameter(param3, param4, param5, param6);
						}
						CutType cType = (CutType)Convert.ToInt32(dataTable.Rows[i]["CutType"]);
						HostType hType = (HostType)Convert.ToInt32(dataTable.Rows[i]["HostType"]);
						string level = Convert.ToString(dataTable.Rows[i]["Level"]);
						string remark = Convert.ToString(dataTable.Rows[i]["Remark"]);
						list.Add(new DbWallToBeOpened(projectId, basePt, wallId, pipeId, sp, bp, cType, hType, level)
						{
							Remark = remark,
							mGuid = mGuid,
							mDatabase = project.Database,
							Status = status
						});
					}
					result = list;
				}
			}
			catch (Exception ex)
			{
				if (project.Database.Connection.State == ConnectionState.Open)
				{
					project.Database.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		private FamilySymbol LoadFitOpeningSymbol(Autodesk.Revit.DB.Document doc, string version)
		{
			string strFamilyName = "";
			if (base.OpeningParameter.SectionType == DBService.SectionType.Arc)
			{
				strFamilyName = "墙洞口-圆形";
			}
			else if (base.OpeningParameter.SectionType == DBService.SectionType.Rect)
			{
				strFamilyName = (this.bEquipOpening ? "墙洞口-设备" : "墙洞口-矩形");
			}
			return DbOpeningBase.LoadOpeningFamily(doc, strFamilyName);
		}

		public override void AddOpeningTag(UIApplication uiApp, TagStyle ts)
		{
			if (this.bEquipOpening)
			{
				this.AddEquipOpeningTag(uiApp, ts);
				return;
			}
			Document document = uiApp.ActiveUIDocument.Document;
			XYZ basisZ = XYZ.BasisZ;
			XYZ basisX = basisZ.CrossProduct(this.vtWallOrient);
			double num = AssistFunc.mmToFeet(50.0);
			XYZ origin = base.Location + this.vtWallOrient * (this.dWallWidth * 0.5 + num);
			XYZ xyz = this.vtWallOrient.Negate().Normalize();
			xyz *= this.dWallWidth * 0.5 + num;
			XYZ origin2 = base.Location + xyz;
			Plane plane = RevitVersionFuncs.CreatePlanByOriginAndBasis(origin, basisX, basisZ);
			Plane plane2 = RevitVersionFuncs.CreatePlanByOriginAndBasis(origin2, basisX, basisZ);
			List<ModelCurve> list = new List<ModelCurve>();
			if (base.SectionParameter.SectionType == DBService.SectionType.Rect)
			{
				double dHalfW = AssistFunc.mmToFeet(base.SectionParameter.Parameter2 * 0.5 + base.BoxParameter.Parameter1);
				double dHalfH = AssistFunc.mmToFeet(base.SectionParameter.Parameter1 * 0.5 + base.BoxParameter.Parameter1);
				Transaction transaction = new Transaction(document, "CreateSketchPlane");
				transaction.Start();
				try
				{
					SketchPlane sketchPlane = RevitVersionFuncs.NewSketchPlane(document, plane);
					SketchPlane sketchPlane2 = RevitVersionFuncs.NewSketchPlane(document, plane2);
					transaction.Commit();
					list.AddRange(base.CreateRectOpeningCvs(document, base.Guid.ToString(), dHalfW, dHalfH, sketchPlane));
					list.AddRange(base.CreateRectOpeningCvs(document, base.Guid.ToString(), dHalfW, dHalfH, sketchPlane2));
					goto IL_268;
				}
				catch
				{
					transaction.RollBack();
					goto IL_268;
				}
			}
			double dR = AssistFunc.mmToFeet(base.SectionParameter.Parameter1 * 0.5 + base.BoxParameter.Parameter1);
			Transaction transaction2 = new Transaction(document, "CreateSketchPlane");
			transaction2.Start();
			try
			{
				SketchPlane sketchPlane3 = RevitVersionFuncs.NewSketchPlane(document, plane);
				SketchPlane sketchPlane4 = RevitVersionFuncs.NewSketchPlane(document, plane2);
				transaction2.Commit();
				list.AddRange(base.CreateCircleOpeningCvs(document, base.Guid.ToString(), dR, sketchPlane3));
				list.AddRange(base.CreateCircleOpeningCvs(document, base.Guid.ToString(), dR, sketchPlane4));
			}
			catch
			{
				transaction2.RollBack();
			}
			IL_268:
			ElementSet elementSet = new ElementSet();
			foreach (ModelCurve modelCurve in list)
			{
				elementSet.Insert(modelCurve);
			}
			if (elementSet.Size > 0)
			{
				Transaction transaction3 = new Transaction(document, "CreateGroup");
				transaction3.Start();
				try
				{
					RevitVersionFuncs.NewGroup(document, elementSet).GroupType.Name = "协同开洞标记_" + base.Guid.ToString();
					transaction3.Commit();
				}
				catch
				{
					transaction3.RollBack();
				}
			}
		}

		public void AddEquipOpeningTag(UIApplication uiApp, TagStyle ts)
		{
			Document document = uiApp.ActiveUIDocument.Document;
			XYZ basisZ = XYZ.BasisZ;
			XYZ xyz = this.vtWallOrient.CrossProduct(basisZ).Normalize();
			double num = AssistFunc.mmToFeet(50.0);
			XYZ xyz2 = base.Location;
			xyz2 -= this.vtWallOrient * num;
			XYZ xyz3 = this.vtWallOrient * (2.0 * num + AssistFunc.mmToFeet(this.dOpeningThickness));
			XYZ xyz4 = xyz2 + xyz3;
			Plane plane = RevitVersionFuncs.CreatePlanByOriginAndBasis(xyz2, xyz, basisZ);
			Plane plane2 = RevitVersionFuncs.CreatePlanByOriginAndBasis(xyz4, xyz, basisZ);
			List<ModelCurve> list = new List<ModelCurve>();
			double num2 = AssistFunc.mmToFeet(base.SectionParameter.Parameter2);
			double num3 = AssistFunc.mmToFeet(base.SectionParameter.Parameter1);
			double num4 = AssistFunc.mmToFeet(base.BoxParameter.Parameter1);
			Transaction transaction = new Transaction(document, "CreateSketchPlane");
			transaction.Start();
			try
			{
				SketchPlane sketchPlane = RevitVersionFuncs.NewSketchPlane(document, plane);
				XYZ xyz5 = xyz2 - xyz * (num2 * 0.5 + num4);
				xyz5 -= basisZ * num4;
				XYZ xyz6 = xyz5 + basisZ * (num3 + num4 * 2.0);
				XYZ xyz7 = xyz6 + xyz * (num2 + num4 * 2.0);
				XYZ xyz8 = xyz5 + xyz * (num2 + num4 * 2.0);
				CurveArray curveArray = new CurveArray();
				curveArray.Append(Line.CreateBound(xyz5, xyz8));
				curveArray.Append(Line.CreateBound(xyz8, xyz7));
				curveArray.Append(Line.CreateBound(xyz7, xyz6));
				curveArray.Append(Line.CreateBound(xyz6, xyz5));
				foreach (object obj in document.Create.NewModelCurveArray(curveArray, sketchPlane))
				{
					ModelCurve item = (ModelCurve)obj;
					list.Add(item);
				}
				SketchPlane sketchPlane2 = RevitVersionFuncs.NewSketchPlane(document, plane2);
				XYZ xyz9 = xyz4 - xyz * (num2 * 0.5 + num4);
				xyz9 -= basisZ * num4;
				XYZ xyz10 = xyz9 + basisZ * (num3 + num4 * 2.0);
				XYZ xyz11 = xyz10 + xyz * (num2 + num4 * 2.0);
				XYZ xyz12 = xyz9 + xyz * (num2 + num4 * 2.0);
				CurveArray curveArray2 = new CurveArray();
				curveArray2.Append(Line.CreateBound(xyz9, xyz12));
				curveArray2.Append(Line.CreateBound(xyz12, xyz11));
				curveArray2.Append(Line.CreateBound(xyz11, xyz10));
				curveArray2.Append(Line.CreateBound(xyz10, xyz9));
				foreach (object obj2 in document.Create.NewModelCurveArray(curveArray2, sketchPlane2))
				{
					ModelCurve item2 = (ModelCurve)obj2;
					list.Add(item2);
				}
				transaction.Commit();
			}
			catch
			{
				transaction.RollBack();
			}
			ElementSet elementSet = new ElementSet();
			foreach (ModelCurve modelCurve in list)
			{
				elementSet.Insert(modelCurve);
			}
			if (elementSet.Size > 0)
			{
				Transaction transaction2 = new Transaction(document, "CreateGroup");
				transaction2.Start();
				try
				{
					RevitVersionFuncs.NewGroup(document, elementSet).GroupType.Name = "协同开洞标记_" + base.Guid.ToString();
					transaction2.Commit();
				}
				catch
				{
					transaction2.RollBack();
				}
			}
		}

		private string FormatWallOpeningFamilyName(UIApplication uiApp)
		{
			string result = null;
			try
			{
				if (this.bEquipOpening)
				{
					result = "墙洞口-设备";
					return result;
				}
				Wall wall = uiApp.ActiveUIDocument.Document.GetElement(new ElementId(base.HostId)) as Wall;
				if (wall == null)
				{
					return null;
				}
				double dAngR = CmdTools.GetPipe2WallHAng(base.PipeStartPoint, base.PipeEndPoint, wall, base.Location);
				double dAngR2 = CmdTools.GetPipe2WallVAng(base.PipeStartPoint, base.PipeEndPoint, wall, base.Location);
				dAngR = CmdTools.R2A(dAngR);
				dAngR2 = CmdTools.R2A(dAngR2);
				double num = base.SectionParameter.Parameter2 + base.BoxParameter.Parameter1;
				double num2 = base.SectionParameter.Parameter1 + base.BoxParameter.Parameter1;
				string text = "";
                DBService.SectionType sectionType = base.SectionParameter.SectionType;
                if (sectionType != DBService.SectionType.Rect)
				{
					if (sectionType == DBService.SectionType.Arc)
					{
						text = "圆形墙洞";
					}
				}
				else
				{
					text = "矩形墙洞";
				}
				result = string.Concat(new string[]
				{
					text,
					"_HAng",
					dAngR.ToString("f2"),
					"_VAng",
					dAngR2.ToString("f2"),
					"_W",
					num.ToString("f2"),
					"_H",
					num2.ToString("f2")
				});
			}
			catch
			{
			}
			return result;
		}

		public override Element Create(UIApplication uiApp)
		{
			Document document = uiApp.ActiveUIDocument.Document;
			if (this.bEquipOpening)
			{
				return this.CreateEquipWallOpening(uiApp);
			}
			FilledRegion elem = this.CreateWallOpening2D(document);
			Element result;
			try
			{
				Wall wall = RevitVersionFuncs.GetElement(document, new ElementId(this.mHostId)) as Wall;
				FamilySymbol familySymbol = this.LoadFitOpeningSymbol(document, document.Application.VersionNumber);
				if (familySymbol == null)
				{
					result = null;
				}
				else
				{
					string text = this.FormatWallOpeningFamilyName(uiApp);
					Family family = ElementFiltering.FindFamily(document, text);
					FamilySymbol familySymbol2;
					if (family == null)
					{
						double pipe2WallHAng = CmdTools.GetPipe2WallHAng(base.PipeStartPoint, base.PipeEndPoint, wall, base.Location);
						double pipe2WallVAng = CmdTools.GetPipe2WallVAng(base.PipeStartPoint, base.PipeEndPoint, wall, base.Location);
						familySymbol2 = CmdTools.CreateFamilySymbolByTemplate(familySymbol.Family, text, pipe2WallHAng, pipe2WallVAng, this);
					}
					else
					{
						familySymbol2 = family.GetSymbols().FirstOrDefault<FamilySymbol>();
					}
					FamilyInstance familyInstance = null;
					if (familySymbol2 != null)
					{
						Transaction transaction = new Transaction(document, "CreateOpening");
						transaction.Start();
						try
						{
							UnifiedModified.ActivateFamilySymbol(familySymbol2);
							Level level = document.GetElement(wall.LevelId) as Level;
							familyInstance = document.Create.NewFamilyInstance(this.mPt, familySymbol2, wall, level, (Autodesk.Revit.DB.Structure.StructuralType)0);
							transaction.Commit();
							if (familyInstance != null)
							{
								base.Status = OpeningStatus.Opened;
								base.SetDate(familyInstance);
								CmdTools.WriteToElement(elem, DbOpeningBase.guidOpening3D, familyInstance.Id.IntegerValue);
								this.AddSharedParameter(familyInstance);
								base.OpeningID = familyInstance.Id;
							}
						}
						catch
						{
							transaction.RollBack();
						}
					}
					result = familyInstance;
				}
			}
			catch (Exception)
			{
				result = null;
			}
			return result;
		}

		private Element CreateEquipWallOpening(UIApplication uiApp)
		{
			try
			{
				Document document = uiApp.ActiveUIDocument.Document;
				ElementId elementId = new ElementId(base.HostId);
				Wall wall = document.GetElement(elementId) as Wall;
				Curve curve = (wall.Location as LocationCurve).Curve;
				XYZ basisZ = XYZ.BasisZ;
				XYZ xyz = this.vtWallOrient.Normalize().Negate();
				XYZ xyz2 = xyz.CrossProduct(basisZ).Normalize();
				double num = AssistFunc.mmToFeet(base.SectionParameter.Parameter2);
				double num2 = AssistFunc.mmToFeet(base.SectionParameter.Parameter1);
				double num3 = AssistFunc.mmToFeet(base.BoxParameter.Parameter1);
				double num4 = AssistFunc.mmToFeet(this.dOpeningThickness);
				XYZ location = base.Location;
				XYZ xyz3 = location - xyz2 * (num * 0.5 + num3);
				XYZ xyz4 = location + xyz2 * (num * 0.5 + num3);
				XYZ xyz5 = xyz3 + xyz * num4;
				XYZ xyz6 = xyz4 + xyz * num4;
				List<CurveLoop> list = new List<CurveLoop>();
				CurveLoop curveLoop = new CurveLoop();
				curveLoop.Append(Line.CreateBound(xyz5, xyz3));
				curveLoop.Append(Line.CreateBound(xyz3, xyz4));
				curveLoop.Append(Line.CreateBound(xyz4, xyz6));
				curveLoop.Append(Line.CreateBound(xyz6, xyz5));
				list.Add(curveLoop);
				FilledRegion filledRegion = null;
				Transaction transaction = new Transaction(document, "CreateFilledRegion");
				transaction.Start();
				try
				{
					ViewPlan viewPlanByName = base.GetViewPlanByName(document);
					if (viewPlanByName != null)
					{
						filledRegion = FilledRegion.Create(document, CmdCreateOpening.m_filledRegionType.Id, viewPlanByName.Id, list);
						filledRegion.SetLineStyleId(CmdCreateOpening.m_lineStyleOpening2D.Id);
						viewPlanByName.SetYJKProjLinePatternOverrideByElement(new List<ElementId>
						{
							filledRegion.Id
						}, CmdCreateOpening.m_linePattern);
					}
					transaction.Commit();
				}
				catch
				{
					transaction.RollBack();
				}
				string familyName = this.FormatWallOpeningFamilyName(uiApp);
				Family family = ElementFiltering.FindFamily(document, familyName);
				FamilySymbol familySymbol;
				if (family == null)
				{
					familySymbol = this.LoadFitOpeningSymbol(document, document.Application.VersionNumber);
				}
				else
				{
					familySymbol = family.GetSymbols().FirstOrDefault<FamilySymbol>();
				}
				if (familySymbol != null)
				{
					FamilyInstance familyInstance = null;
					if (familySymbol != null)
					{
						Transaction transaction2 = new Transaction(document, "CreateOpening");
						transaction2.Start();
						UnifiedModified.ActivateFamilySymbol(familySymbol);
						AssistFunc.mmToFeet(base.BoxParameter.Parameter1);
						Level level = document.GetElement(wall.LevelId) as Level;
						familyInstance = document.Create.NewFamilyInstance(this.mPt - XYZ.BasisZ * num3, familySymbol, wall, level, (Autodesk.Revit.DB.Structure.StructuralType)0);
						familyInstance.GetParameter("宽度").Set(num + num3 * 2.0);
						familyInstance.GetParameter("高度").Set(num2 + num3 * 2.0);
						familyInstance.GetParameter("厚度").Set(num4);
						document.Regenerate();
						if (this.vtWallOrient.IsSameDirectionTo(familyInstance.FacingOrientation))
						{
							Plane plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(wall.Orientation, (wall.Location as LocationCurve).Curve.GetEndPoint(0));
							familyInstance = MirrorOperater.MirrorParasiticElement(document, familyInstance, plane, false);
						}
						transaction2.Commit();
					}
					base.Status = OpeningStatus.Opened;
					base.SetDate(familyInstance);
					CmdTools.WriteToElement(filledRegion, DbOpeningBase.guidOpening3D, familyInstance.Id.IntegerValue);
					this.AddSharedParameter(familyInstance);
					base.OpeningID = familyInstance.Id;
					return familyInstance;
				}
			}
			catch
			{
			}
			return null;
		}

		private void AddSharedParameter(Element elem)
		{
			Document document = elem.Document;
			Transaction transaction = new Transaction(document, "AddSharedParameter");
			try
			{
				transaction.Start();
				List<string> list = new List<string>();
				list.Add(DbOpeningBase.m_strSharedParaOpeningMajor);
				list.Add(DbOpeningBase.m_strSharedParaOpeningSize);
				list.Add(DbOpeningBase.m_strSharedParaOpeningElevTop);
				list.Add(DbOpeningBase.m_strSharedParaOpeningElevCenter);
				list.Add(DbOpeningBase.m_strSharedParaOpeningElevBottom);
				list.Add(DbOpeningBase.m_strSharedParaOpeningElev);
                BuiltInCategory integerValue = (BuiltInCategory)elem.Category.Id.IntegerValue;
				Dictionary<string, ParameterType> dictionary = new Dictionary<string, ParameterType>();
				foreach (string text in list)
				{
					if (elem.GetParameter(text) == null)
					{
                        dictionary.Add(text, (ParameterType)1);
					}
				}
				if (dictionary.Count > 0)
				{
					SharedParameter.InsertSharedParameter(document, integerValue, "洞口信息", dictionary, false, true);
				}
				Parameter parameter = elem.GetParameter(DbOpeningBase.m_strSharedParaOpeningMajor);
				if (parameter != null)
				{
					parameter.Set(base.MajorString);
				}
				parameter = elem.GetParameter(DbOpeningBase.m_strSharedParaOpeningSize);
				if (parameter != null)
				{
					string text2;
					if (base.SectionParameter.SectionType == DBService.SectionType.Arc)
					{
						text2 = "φ" + (base.SectionParameter.Parameter1 + base.BoxParameter.Parameter1 * 2.0).ToString("F0");
					}
					else
					{
						double num = base.SectionParameter.Parameter2 + base.BoxParameter.Parameter1 * 2.0;
						text2 = (base.SectionParameter.Parameter1 + base.BoxParameter.Parameter1 * 2.0).ToString("F0") + "X" + num.ToString("F0");
					}
					parameter.Set(text2);
				}
				double num2 = AssistFunc.feetToMM(base.Location.Z) / 1000.0;
				double num3 = num2 + base.SectionParameter.Parameter1 * 0.5 / 1000.0;
				double num4 = num2 - base.SectionParameter.Parameter1 * 0.5 / 1000.0;
				parameter = elem.GetParameter(DbOpeningBase.m_strSharedParaOpeningElevTop);
				if (parameter != null)
				{
					string text3 = "顶部标高" + num3.ToString("F3");
					parameter.Set(text3);
				}
				parameter = elem.GetParameter(DbOpeningBase.m_strSharedParaOpeningElevCenter);
				if (parameter != null)
				{
					string text4 = "中心标高" + num2.ToString("F3");
					parameter.Set(text4);
				}
				parameter = elem.GetParameter(DbOpeningBase.m_strSharedParaOpeningElevBottom);
				if (parameter != null)
				{
					string text5 = "底部标高" + num4.ToString("F3");
					parameter.Set(text5);
				}
				parameter = elem.GetParameter(DbOpeningBase.m_strSharedParaOpeningElev);
				if (parameter != null)
				{
					parameter.Set("");
				}
				transaction.Commit();
			}
			catch
			{
				transaction.RollBack();
			}
		}

		private FilledRegion CreateWallOpening2D(Autodesk.Revit.DB.Document doc)
		{
			try
			{
				ElementId elementId = new ElementId(base.HostId);
				Wall wall = doc.GetElement(elementId) as Wall;
				Curve curve = (wall.Location as LocationCurve).Curve;
				double width = wall.WallType.Width;
				XYZ xyz = (base.PipeEndPoint - base.PipeStartPoint).Normalize();
				XYZ orientation = wall.Orientation;
				Curve cv = null;
				Curve cv2 = null;
				if (curve is Arc)
				{
					Arc arc = curve as Arc;
					double radius = arc.Radius;
					XYZ center = arc.Center;
					XYZ hyendPoint = arc.GetEndPoint(0);
					XYZ hyendPoint2 = arc.GetEndPoint(1);
					XYZ xyz2 = (hyendPoint + hyendPoint2) * 0.5;
					XYZ xyz3 = XYZ.BasisZ.CrossProduct(hyendPoint2 - hyendPoint).Normalize();
					Curve curve2 = Line.CreateUnbound(xyz2, xyz3);
					IntersectionResultArray intersectionResultArray = null;
                    if ((int)curve2.Intersect(curve, out intersectionResultArray) == 8)
					{
						xyz2 = intersectionResultArray.get_Item(0).XYZPoint;
						XYZ xyz4 = (hyendPoint - center).Normalize() * width * 0.5;
						XYZ xyz5 = (hyendPoint2 - center).Normalize() * width * 0.5;
						XYZ xyz6 = (xyz2 - center).Normalize() * width * 0.5;
						cv = Arc.Create(hyendPoint + xyz4, hyendPoint2 + xyz5, xyz2 + xyz6);
						cv2 = Arc.Create(hyendPoint - xyz4, hyendPoint2 - xyz5, xyz2 - xyz6);
					}
				}
				else
				{
					Transform transform = Transform.CreateTranslation(orientation * width * 0.5);
					cv = curve.CreateTransformed(transform);
					cv2 = curve.CreateTransformed(transform.Inverse);
				}
				XYZ xyz7 = xyz.CrossProduct(XYZ.BasisZ).Normalize();
				double num = base.SectionParameter.Parameter2;
				if (base.SectionParameter.SectionType == DBService.SectionType.Arc)
				{
					num = base.SectionParameter.Parameter1;
				}
				num = AssistFunc.mmToFeet(num);
				Transform transform2 = Transform.CreateTranslation(xyz7 * (num * 0.5 + AssistFunc.mmToFeet(base.BoxParameter.Parameter1)));
				Plane planeProj = RevitVersionFuncs.CreatePlanByNormalAndOrigin(XYZ.BasisZ, XYZ.Zero);
				XYZ xyz8 = CmdTools.CalcProjPointByPlane(xyz, planeProj);
				XYZ origin = new XYZ(0.0, 0.0, wall.GetYJKLevel().Elevation);
				Plane planeProj2 = RevitVersionFuncs.CreatePlanByNormalAndOrigin(XYZ.BasisZ, origin);
				XYZ xyz9 = CmdTools.CalcProjPointByPlane(base.Location, planeProj2);
				XYZ xyz10 = xyz9 + xyz8 * width * 3.0;
				XYZ xyz11 = xyz9 - xyz8 * width * 3.0;
				Line line = Line.CreateBound(xyz10, xyz11);
				Curve cv3 = line.CreateTransformed(transform2);
				Curve cv4 = line.CreateTransformed(transform2.Inverse);
				XYZ curveInterPoint = base.GetCurveInterPoint(cv, cv4);
				XYZ curveInterPoint2 = base.GetCurveInterPoint(cv, cv3);
				XYZ curveInterPoint3 = base.GetCurveInterPoint(cv2, cv4);
				XYZ curveInterPoint4 = base.GetCurveInterPoint(cv2, cv3);
				List<CurveLoop> list = new List<CurveLoop>();
				CurveLoop curveLoop = new CurveLoop();
				curveLoop.Append(Line.CreateBound(curveInterPoint, curveInterPoint3));
				curveLoop.Append(Line.CreateBound(curveInterPoint3, curveInterPoint4));
				curveLoop.Append(Line.CreateBound(curveInterPoint4, curveInterPoint2));
				curveLoop.Append(Line.CreateBound(curveInterPoint2, curveInterPoint));
				list.Add(curveLoop);
				Transaction transaction = new Transaction(doc, "CreateFilledRegion");
				transaction.Start();
				try
				{
					FilledRegion filledRegion = null;
					ViewPlan viewPlanByName = base.GetViewPlanByName(doc);
					if (viewPlanByName != null)
					{
						filledRegion = FilledRegion.Create(doc, CmdCreateOpening.m_filledRegionType.Id, viewPlanByName.Id, list);
						filledRegion.SetLineStyleId(CmdCreateOpening.m_lineStyleOpening2D.Id);
						viewPlanByName.SetYJKProjLinePatternOverrideByElement(new List<ElementId>
						{
							filledRegion.Id
						}, CmdCreateOpening.m_linePattern);
					}
					transaction.Commit();
					return filledRegion;
				}
				catch
				{
					transaction.RollBack();
				}
			}
			catch
			{
			}
			return null;
		}

		public static DbWallToBeOpened GetFromDB(Guid id, BimServerDB db)
		{
			DbWallToBeOpened result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				DbCommand dbCommand = db.Connection.CreateCommand();
				dbCommand.CommandText = string.Format("SELECT ProjectId,HostId,PtX,PtY,PtZ,Parameter1,Parameter2,SectionType,Guid,Status,BoxParameter1,BoxParameter2,BoxParameter3,BoxParameter4,Level,CutType,HostType,BoxSectionType,Remark,PipeId FROM tbSynergyOpiening WHERE Guid={0} and (HostType=0 or HostType=2)", "@id");
				SqlParameter sqlParameter = new SqlParameter("@id", SqlDbType.UniqueIdentifier);
				sqlParameter.Value = id;
				dbCommand.Parameters.Add(sqlParameter);
				IDataReader dataReader = dbCommand.ExecuteReader();
				DataTable dataTable = db.DataReaderToDataTable(dataReader);
				dataReader.Dispose();
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				if (dataTable == null)
				{
					result = null;
				}
				else
				{
					int projectId = Convert.ToInt32(dataTable.Rows[0]["ProjectId"]);
					int wallId = Convert.ToInt32(dataTable.Rows[0]["HostId"]);
					int pipeId = Convert.ToInt32(dataTable.Rows[0]["PipeId"]);
					OpeningStatus status = (OpeningStatus)dataTable.Rows[0]["Status"];
					XYZ basePt = new XYZ(Convert.ToDouble(dataTable.Rows[0]["PtX"]), Convert.ToDouble(dataTable.Rows[0]["PtY"]), Convert.ToDouble(dataTable.Rows[0]["PtZ"]));
					DBService.SectionType sectionType = (DBService.SectionType)Convert.ToInt32(dataTable.Rows[0]["SectionType"]);
					SectionParameter sp = null;
					if (sectionType == DBService.SectionType.Arc)
					{
						sp = new SectionParameter(Convert.ToDouble(dataTable.Rows[0]["Parameter1"]));
					}
					else if (sectionType == DBService.SectionType.Rect)
					{
						double param = Convert.ToDouble(dataTable.Rows[0]["Parameter1"]);
						double param2 = Convert.ToDouble(dataTable.Rows[0]["Parameter2"]);
						sp = new SectionParameter(param, param2);
					}
					DBService.SectionType sectionType2 = (DBService.SectionType)Convert.ToInt32(dataTable.Rows[0]["BoxSectionType"]);
					BoxParameter bp = null;
					if (sectionType2 == DBService.SectionType.Arc)
					{
						bp = new BoxParameter(Convert.ToDouble(dataTable.Rows[0]["BoxParameter1"]));
					}
					else if (sectionType2 == DBService.SectionType.Rect)
					{
						double param3 = Convert.ToDouble(dataTable.Rows[0]["BoxParameter1"]);
						double param4 = Convert.ToDouble(dataTable.Rows[0]["BoxParameter1"]);
						double param5 = Convert.ToDouble(dataTable.Rows[0]["BoxParameter3"]);
						double param6 = Convert.ToDouble(dataTable.Rows[0]["BoxParameter4"]);
						bp = new BoxParameter(param3, param4, param5, param6);
					}
					CutType cType = (CutType)Convert.ToInt32(dataTable.Rows[0]["CutType"]);
					HostType hType = (HostType)Convert.ToInt32(dataTable.Rows[0]["HostType"]);
					string level = Convert.ToString(dataTable.Rows[0]["Level"]);
					string remark = Convert.ToString(dataTable.Rows[0]["Remark"]);
					result = new DbWallToBeOpened(projectId, basePt, wallId, pipeId, sp, bp, cType, hType, level)
					{
						Remark = remark,
						mGuid = id,
						mDatabase = db,
						Status = status
					};
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public override bool Equals(object obj)
		{
			if (obj is DbWallToBeOpened)
			{
				DbWallToBeOpened dbWallToBeOpened = (DbWallToBeOpened)obj;
				return dbWallToBeOpened.mPt.IsAlmostEqualTo(this.mPt) && dbWallToBeOpened.mHostId == this.mHostId;
			}
			return false;
		}

		public XYZ vtWallOrient;

		public double dWallWidth;

		public bool bEquipOpening;

		public double dOpeningThickness;
	}
}
